//
//  MateMapViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/10/4.
//

#import "MateAddressMapViewController.h"
#import "BaseAnntation.h"
#import "SQMKAnnotationView.h"
@interface MateAddressMapViewController ()<MKMapViewDelegate>
@property (nonatomic , strong) MKMapView * mapView;
@property (nonatomic , strong) HeaderView * addButton;

@end

@implementation MateAddressMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"集会地点");
    self.zx_navBarBackgroundColor = SQHexColor(@"#F1F1F1", 0.5);
}

- (void)SQ_UIViewForSelfView{
    [self.view addSubview:self.mapView];
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake([self.model.lat floatValue], [self.model.lng floatValue]);
       float zoomLevel = 0.005;
       MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, MKCoordinateSpanMake(zoomLevel, zoomLevel));
    BaseAnntation *pointAnnotation = [[BaseAnntation alloc] init];
    pointAnnotation.coordinate =  coordinate2D;
    pointAnnotation.title = self.model.address;
    [self.mapView addAnnotation:pointAnnotation];
    [self.mapView showAnnotations:@[pointAnnotation] animated:YES];
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
    
    [self.view addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20-FootHeight);
        make.height.mas_equalTo(48);
    }];
}

-(void)SQ_addAnntionForMap{
    
    //高德地图
    NSURL *gaode_App = [NSURL URLWithString:@"iosamap://"];
    NSURL *apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
    if ([[UIApplication sharedApplication] canOpenURL:gaode_App]) {
        // 使用高德地图导航
         NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=wiseweb.FightEagle&dlat=%@&dlon=%@&dname=%@&style=2&t=0&dev=0",self.model.lat,self.model.lng,self.model.address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
      //   使用苹果自带地图导航
        NSString *urlString=[[NSString stringWithFormat:@"http://maps.apple.com/?daddr=%@",self.model.address] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }
 
    
}


//设置大头针
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    if (![annotation isKindOfClass:[BaseAnntation class]]) {// 过滤掉MKUserLocation
        return nil;
    }
    BaseAnntation* ann = (BaseAnntation *)annotation;
    SQMKAnnotationView *annotationView = (SQMKAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"map"];
    if (!annotationView) {
       annotationView = [[SQMKAnnotationView alloc] initWithAnnotation:ann
                                                     reuseIdentifier:@"map"];
        annotationView.opaque = NO;
    }
    annotationView.addressLabel.text = self.model.address;
    return annotationView;
}

- (void)dealloc {
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode  = MKUserTrackingModeNone;
    [_mapView.layer removeAllAnimations];
    [_mapView removeAnnotations:_mapView.annotations];
    [_mapView removeOverlays:_mapView.overlays];
    [_mapView removeFromSuperview];
    _mapView.delegate = nil;
    _mapView = nil;
}


/** 懒加载地图视图 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
        /** 设置地图类型 */
        _mapView.mapType = MKMapTypeStandard;
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //设置地图可缩放
        _mapView.zoomEnabled = YES;
        _mapView.layer.cornerRadius = 12;
        _mapView.layer.masksToBounds = YES;
        //设置地图可滚动
        _mapView.scrollEnabled = YES;
        //设置地图可旋转
        _mapView.rotateEnabled = YES;
        //设置显示用户显示位置
        _mapView.showsUserLocation = YES;
        //为MKMapView设置delegate
        _mapView.delegate = self;
        _mapView.showsBuildings = YES;
        _mapView.showsTraffic = YES;
    }
    return _mapView;
}

- (HeaderView *)addButton{
    if (!_addButton) {
        _addButton = [[HeaderView alloc]init];
        _addButton.backgroundColor  = SQColor3DA59D;
        _addButton.colorString = @"#FFFFFF";
        [_addButton.btn addTarget:self action:@selector(SQ_addAnntionForMap) forControlEvents:UIControlEventTouchUpInside];
        _addButton.layer.cornerRadius = 6;
        _addButton.layer.masksToBounds = YES;
        _addButton.text = SQNSLocalString(@"导航");
        _addButton.font = FONT_MEDIUM(18);
    }
    return _addButton;
}

@end
